package cn.edu.hunau.thread;

import java.time.LocalTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

/**
 * @author hb
 * @date 2025-08-18 10:23:59
 */
public class TestInterrupt {
    public static void main(String[] args) {
        Thread t1=new Thread("t-01"){
            @Override
            public void run() {
                 for(int i = 0; i < 10; i++){
                     System.out.println("\t"+getName()+"正在运行 i="+i+" time="+ LocalTime.now());
                     if (i%4==0) {
                         try {
                            // Thread.sleep(20000);
                             TimeUnit.SECONDS.sleep(2);
                         } catch (InterruptedException e) {
                             System.out.println("【休眠被终端】");
                             throw new RuntimeException(e);
                         }
                     }
                 }
            }
        };


        Thread t2=  new Thread("t-02"){
            @Override
            public void run() {
                  for (int i = 0; i < 12; i++){
                      System.out.println(getName()+"正在运行 i="+i+" time="+ LocalTime.now());
                      if ((i+1)%7==0){
                          //提前终端t1的休眠
                          t1.interrupt();
                      }
                  }
            }
        };

        t1.start();
        t2.start();
    }
}
